package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.blog.vo.ArticleTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@rc
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<ArticleType>> findAll() {
        List<ArticleType> articleTypes = articleTypeService.findAll();
        System.out.println(articleTypes);
        return R.ok(articleTypes);//alt enter生成返回值
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<ArticleType> findOne(@PathVariable Long id) {
        ArticleType articleType = articleTypeService.findOne(id);
        return R.ok(articleType);//alt enter生成返回值
    }

    @PostMapping
    @ApiOperation(value = "分页+条件查询")
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        List<ArticleType> list = articleTypeService.queryPage(articleTypeQuery);
        return new PageInfo<ArticleType>(list);//alt enter生成返回值,list分页集合
    }

    @PutMapping
    @ApiOperation(value = "增加or修改")
    public R<Void> addOrUpdate(@RequestBody ArticleType articleType) {
        //try
        try {
            Long id = articleType.getId();

            if (id==null){
                articleTypeService.addArticleType(articleType);
            }else {
                articleTypeService.updateArticleType(articleType);
            }
        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteArticleType(@PathVariable Long id) {
        //try
        try {
            articleTypeService.deleteArticleType(id);
        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchArticleTypes(@RequestBody Long[] ids) {
        try {
            articleTypeService.batchArticleTypes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/getTypeTree")
    @ApiOperation(value = "查询类型树")
    public R<List<ArticleType>> getTypeTree() {
        List<ArticleType> articleTypes = articleTypeService.getTypeTree();//假设法
        return R.ok(articleTypes);//alt enter生成返回值
    }
    @GetMapping("/articleTypeStatistics")
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        List<ArticleTypeVo> articleTypeVos = articleTypeService.getArticleTypeStatistics();//假设
        return articleTypeVos;
    }
}
